package com.jzr.pda.controller.api;

import com.jzr.common.BaseController;
import com.jzr.common.entity.vo.Result;
import com.jzr.pda.auth.JwtTokenProvider;
import com.jzr.pda.db1.service.TUserService;
import com.jzr.pda.db1.vo.DeptCodeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@Api(tags = "用户接口")
@RestController
@RequestMapping(value = "/api/user")
public class UserController extends BaseController {
    @Autowired
    private TUserService userService;

    @Autowired
    private JwtTokenProvider tokenProvider;

    @GetMapping("/refesh")
    @ApiOperation("refesh -> 刷新token")
    public Result<String> refeshToken() throws IOException {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return Result.success("Refesh成功", tokenProvider.createJwtToken(authentication));
    }

    @PostMapping("/logout")
    @ApiOperation("logout -> 注销登录")
    public Result<Boolean> logout(HttpServletRequest req) throws IOException {
        HttpSession session = req.getSession(false);
        if (session != null) {
            session.invalidate();
        }
        return Result.success("Logout成功", true);
    }

    @GetMapping("/deptList")
    @ApiOperation("用户的部门列表")
    public Result<List<DeptCodeVo>> deptList(String userCode){
        List<DeptCodeVo> list = userService.getDeptList(userCode);
        return Result.success("成功",list);
    }

}